try:
    import gdal
except:
    from osgeo import gdal
import re 
import os
import math
import datetime

def WindowsClipRaster(
    raster_path, window, step, save_dir, model="overlap"):
    name = os.path.splitext(os.path.basename(raster_path))[0]
    dataset = gdal.Open(raster_path)
    projection = dataset.GetProjection()
    geotransform = dataset.GetGeoTransform()
    width, height = dataset.RasterXSize, dataset.RasterYSize
    xmin, xsize = geotransform[0], geotransform[1]
    ymax, ysize = geotransform[3], geotransform[5]
    if model == "overlap":
        xsteps = math.ceil((width-window[0])/step[0])+1
        ysteps = math.ceil((height-window[1])/step[1])+1
        for xindex in range(xsteps):
            if xindex < xsteps - 1:
                left = xmin + xsize * step[0] * xindex 
            else:
                left = xmin + (width - window[0]) * xsize
            right = left + window[0] * xsize
            for yindex in range(ysteps):
                if yindex < ysteps - 1:
                    top = ymax + ysize * step[1] * yindex
                else:
                    top = ymax + (height - window[1]) * ysize
                bottom = top + window[1] * ysize
                outputBounds = (left, bottom, right, top)
                raster_name = f"{name}_{xindex}_{yindex}.tif"
                print(f"{datetime.datetime.now()}:正在生成>{raster_name}")
                if not os.path.exists(os.path.join(save_dir, raster_name)):
                    gdal.Warp(
                        os.path.join(save_dir, raster_name), 
                        raster_path, 
                        format='GTiff', 
                        outputBounds=outputBounds, 
                        outputBoundsSRS=projection, 
                        multithread=20)
    else:
        xsteps = (width - window[0]) // step[0] + 1
        ysteps = (height - window[1]) // step[1] + 1
        for xindex in range(xsteps):
            left = xmin + xsize * step[0] * xindex
            right=left+window[0]*xsize if xsteps-1>xindex else xmin
            for yindex in range(ysteps):
                top = ymax + ysize * step[1] * yindex
                if yindex < ysteps - 1:
                    bottom = top + window[1] * ysize
                else:
                    bottom = ymax + height * ysize
                outputBounds = (left, bottom, right, top)
                raster_name = f"{name}_{xindex}_{yindex}.tif"
                print(f"{datetime.datetime.now()}:正在生成>{raster_name}")
                if not os.path.exists(os.path.join(save_dir, raster_name)):
                    gdal.Warp(
                        os.path.join(save_dir, raster_name), 
                        raster_path, 
                        format='GTiff', 
                        outputBounds=outputBounds, 
                        outputBoundsSRS=projection, 
                        multithread=20)
    return

if __name__ == "__main__":
    window = (10240, 10240)
    step = (10240, 10240)
    folder = r"Z:\orange\png"
    save_dir = r"Z:\orange\clip"
    for name in os.listdir(folder):
        if re.match(".*\.png$", name):
            raster_path = os.path.join(folder, name)
            folder_name = os.path.splitext(name)[0]
            clip_folder = os.path.join(save_dir, folder_name)
            if not os.path.exists(clip_folder):
                os.makedirs(clip_folder)
            WindowsClipRaster(
                raster_path, 
                window, 
                step, 
                clip_folder,
                model="overlap")
     